﻿using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

namespace BallDemo
{
    public class BallMovementSystem : ComponentSystem
    {
        public struct Data
        {
            public readonly int Length;
            [ReadOnly] public EntityArray Entities;
            [ReadOnly] public ComponentDataArray<Position> BallPositions;
            [ReadOnly] public ComponentDataArray<Rotation> BallRotations;
            [ReadOnly] public ComponentDataArray<BallDemoTag> Tags;
        }

        [Inject] Data dataArray;

        protected override void OnUpdate()
        {
            for (int i = 0; i < dataArray.Length; i++)
            {
                float3 pos = dataArray.BallPositions[i].Value;
                pos.y += Mathf.Sin(Time.frameCount / Mathf.Rad2Deg);
                PostUpdateCommands.SetComponent(dataArray.Entities[i], new Position() { Value = pos });

                Quaternion rotation = Quaternion.AngleAxis(math.sin(Time.frameCount / Mathf.Rad2Deg) * 100, Vector3.up);
                PostUpdateCommands.SetComponent(dataArray.Entities[i], new Rotation() { Value = rotation });
            }
        }
    }
}